﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using Microsoft.DirectX;

using NGE;
using MH;

namespace MH.SE
{
    class SECOM : SceneComponent
    {
        public FileIndexer CursorImg;
        public Rectangle CursorDrawRect;
        public Vector3 CursorLocation;
        public Rectangle CursorSelectedRect;
        public Vector3 SceneOffset
        {
            set
            {
                SCN.Location = value;
            }
            get
            {
                return SCN.Location;
            }
        }
        public bool DrawCode = false;
        public int DrawCodeIndex = 0;


       
        public SECOM(Game g)
            : base("SceneCOM",g)
        {
            CursorImg = null;

            CursorDrawRect = new Rectangle();
            Bitmap bm = new Bitmap(16,16);

            Graphics gr = Graphics.FromImage(bm);
            gr.DrawString("禁", new Font("宋体", 11), Brushes.White, new PointF());
            nimgx = new NImage();
            nimgx.GetData(bm);

            gr.Clear(Color.Blue);
            gr.DrawString("跳", new Font("宋体", 11), Brushes.Yellow, new PointF());
            nimgo = new NImage();
            nimgo.GetData(bm);

            gr.Clear(Color.Red);
            gr.DrawString("怪", new Font("宋体", 11), Brushes.DarkGray, new PointF());
            nimgm = new NImage();
            nimgm.GetData(bm);

            IsShowExtendTile = false;
        }

        public override void Heartbeat()
        {
            //base.Heartbeat();
            //SCN.Location = SceneOffset;
            game.Timer.Record();
            if (game.Timer.Seconds > SCN.FrameInterval / 1000.0)
            {
                frameIndex++;
                game.Timer.Reset();
            }
        }

        public override void Show()
        {
            //base.Draw();

            //drawTerrain(SceneOffset);
            if (DrawCode == true)
            {
                FilterColor = Color.FromArgb(150, Color.White);
            }
            else
            {
                FilterColor = Color.White;
            }
            drawSCN(SCN, null);

            drawCursor(SceneOffset);
            drawRect(SceneOffset);
            drawCode(SceneOffset);
            drawEnter(SceneOffset);
            drawMZ(SceneOffset);

            //game.Graph.DrawText( SCN.TileImageSet.Count+"", Color.Red, new Vector3());
        }

        public override void OnMouseEventCode(object sender, int x, int y, MouseState ms)
        {
            base.OnMouseEventCode(sender, x, y, ms);

            if (ms == MouseState.Move)
            {
            }
        }
        void drawRect(Vector3 offset)
        {
            Rectangle rect = CursorSelectedRect;
            rect.X += (int)offset.X;
            rect.Y += (int)offset.Y;
            game.Graph.DrawRectangle(Color.White, rect, false);
        }
        void drawCursor(Vector3 offset)
        {
            Vector3 v3 = Util.Vector3AddVector3(offset, CursorLocation);
            if (CursorImg != null)
                game.Graph.DrawImage(CursorImg.FullPathAndFileName, Color.FromArgb(150, Color.White), CursorDrawRect, v3);
        }
        void drawTerrain(Vector3 offset)
        {
            foreach (Terrain.Layer layer in SCN.Terrain.Layers)
            {
                if (layer.Visible)
                {
                    foreach (Terrain.Layer.Tile tile in layer.Tiles)
                    {
                        Vector3 v3 = Util.Vector3AddVector3(offset, tile.Location);
                        if (v3.X < 0 - SCN.TileSize.Width || v3.X > game.Option.WindowSize.Width / game.Option.Zoom || v3.Y < 0 - SCN.TileSize.Height || v3.Y > game.Option.WindowSize.Height / game.Option.Zoom)
                        {
                            continue;
                        }
                        int i = frameIndex % tile.Frames.Count;
                        if (i > -1)
                        {
                            int alpha = 255;
                            if (DrawCode)
                            {
                                alpha = 100;
                            }
                            game.Graph.DrawImage(SCN.TileImageSet[tile.Frames[i].TextureIndexer.Index],Color.FromArgb(alpha,alpha,alpha), v3);
                        }
                    }
                }
            }
        }



        NImage nimgx;
        NImage nimgo;
        NImage nimgm;


        void drawCode(Vector3 offset)
        {
            if (DrawCode == false)
            {
                return;
            }

            for (int y = 0; y < SCN.Size.Height; y++)
            {
                for (int x = 0; x < SCN.Size.Width; x++) 
                {
                    Vector3 v3 = new Vector3(x * SCN.TileSize.Width, y * SCN.TileSize.Height, 0);
                    Vector3 v4 = Util.Vector3AddVector3(v3, offset);

                    if (v4.X < 0 - SCN.TileSize.Width || v4.X > game.Option.WindowSize.Width / game.Option.Zoom || v4.Y < 0 - SCN.TileSize.Height || v4.Y > game.Option.WindowSize.Height / game.Option.Zoom)
                    {
                        continue;
                    }


                    if (SCN.TileCodeLayer[v3] == null)
                    {
                        //game.Graph.DrawImage(nimgo, v4);
                    }
                    else
                    {
                        if (DrawCodeIndex == 0)
                        {
                            if (SCN.TileCodeLayer[v3].CHR)
                            {
                                //game.Graph.DrawImage(nimgo, v4);
                            }
                            else
                            {
                                game.Graph.DrawImage(nimgx, v4);
                            }
                        }else if (DrawCodeIndex == 1)
                        {
                            if (SCN.TileCodeLayer[v3].MTL)
                            {
                                //game.Graph.DrawImage(nimgo, v4);
                            }
                            else
                            {
                                game.Graph.DrawImage(nimgx, v4);
                            }
                        }
                    }
                }
            }
        }
        void drawEnter(Vector3 offset)
        {
            if (DrawCode == false)
            {
                return;
            }
            foreach (EntryCode ec in SCN.EntryCodeLayer.EntryCodeSet)
            {
                for (int y = 0; y < ec.Zone.Height; y += SCN.TileSize.Height)
                {
                    for (int x = 0; x < ec.Zone.Width; x += SCN.TileSize.Width)
                    {
                        Vector3 v3 = new Vector3(ec.Zone.X + x, ec.Zone.Y + y, 0);
                        Vector3 v4 = Util.Vector3AddVector3(v3, offset);
                        game.Graph.DrawImage(nimgo, v4);
                    }
                }
            }
        }
        void drawMZ(Vector3 offset)
        {
            if (DrawCode == false)
            {
                return;
            }
            foreach (MonsterCode mc in SCN.MonsterCodeLayer.MonsterCodeSet)
            {
                for (int y = 0; y < mc.Zone.Height; y += SCN.TileSize.Height)
                {
                    for (int x = 0; x < mc.Zone.Width; x += SCN.TileSize.Width)
                    {
                        Vector3 v3 = new Vector3(mc.Zone.X + x, mc.Zone.Y + y, 0);
                        Vector3 v4 = Util.Vector3AddVector3(v3, offset);
                        game.Graph.DrawImage(nimgm, v4);
                    }
                }
            }
        }
        public override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            //base.OnKeyboardEventCode(sender, key, ks);
        }

    }
}
